<?php

namespace app\admin\controller;

use app\admin\service\ProductCategory as ServiceProductCategory;
use app\Request;
use app\model\ProductCategory as ProductCategoryModel;

class ProductCategory
{
    public function index($page=1,$size=10)
    {
        $list=(new ProductCategoryModel())->getPageData($page,$size);
     
        return success($list->hidden(['delete_time','update_time'])->append(['status_text'])
        ->withAttr('name', function($value, $data) {
            return $value;
        }));
    }

    public function save(Request $request)
    {
        $data = $request->params;
        $res = (new ProductCategoryModel())->save($data);
        if (!$res) {
            return fail($res);
        } else {
            return success($data);
        }
    }
    public function update(Request $request)
    {
        $data = $request->params;
        $exist = !ProductCategoryModel::findOrEmpty($data['id'])->isEmpty();
        if ($exist) {
            $res = ProductCategoryModel::update($data);
           // dump($res . '123');
            die;
            if ($res) {
                return success($data);
            } else {
                return fail();
            }
        } else {
            return fail($data);
        }
    }
    public function delete(Request $request)
    {
        $id = $request->params["id"];
        $res = (new ServiceProductCategory())->deleteByID($id);
        if ($res) {
            return success();
        } else {
            return fail();
        }
    }
    public function read(Request $request)
    {
        $id=$request->params['id'];
        $res=ProductCategoryModel::find($id)->hidden(['create_time'])->toArray();
        if ($res) {
            return success($res);
        } else {
            return fail();
        }
    }
}
